/**
 * @filename:OtherinspectServiceImpl Apr 11, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.OtherInspectDetailDao;
import com.wonder.app.mapper.OtherinspectDao;
import com.wonder.app.service.OtherInspectDetailService;
import com.wonder.app.service.OtherinspectService;
import com.wonder.app.request.OtherinspectRequest;
import com.wonder.app.service.StkOtherInService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(其他入库质检服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class OtherinspectServiceImpl  extends ServiceImpl<OtherinspectDao, Otherinspect> implements OtherinspectService  {

	@Autowired
	private OtherinspectDao otherinspectDao;

	@Autowired
	private OtherInspectDetailDao otherInspectDetailDao;

	@Autowired
	private OtherInspectDetailService otherInspectDetailService;

	@Autowired
	private OtherinspectService otherinspectService;

	@Autowired
	private StkOtherInService otherInService;

    /**
     * 验证其他入库质检信息
     * 
     * @param otherinspect
     * @param type
     * @return String
     */
    @Override
    public String validate(Otherinspect otherinspect,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:其他入库质检分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Otherinspect>   //分页记录
	 */
	@Override 
	public IPage<Otherinspect> selectList(OtherinspectRequest request, Page<Otherinspect> page){
	    LambdaQueryWrapper<Otherinspect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(Otherinspect::getId, request.getId());
	      }               
             
        if (StringUtils.hasText(request.getBillNo())) {
			    lambdaQueryWrapper.like(Otherinspect::getBillNo, request.getBillNo()); 
		    } 
             
        if (StringUtils.hasText(request.getWeightNo())) {
			    lambdaQueryWrapper.like(Otherinspect::getWeightNo, request.getWeightNo()); 
		    } 
             
        
		lambdaQueryWrapper.orderByDesc(Otherinspect::getId);	
		Page<Otherinspect> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Override
	public Map<String, Object> getOtherInspectList(Map<String, Object> map) {
		int pageIndex = VtuzxUtil.getInt(map,"pageIndex");
		int pageSize = VtuzxUtil.getInt(map,"pageSize");
		String billNo = VtuzxUtil.getString(map,"billNo");//订单号
		String weightNo = VtuzxUtil.getString(map,"weightNo");//称重申请单号
		List<Otherinspect> dataList = otherinspectDao.getOtherInspectList(pageSize,pageIndex * pageSize,billNo,weightNo);
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}
	/**
	 * 其他入库质检明细详情
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getOtherInspectDetail(Map<String, Object> map) {
		String billNo = map.get("billNo").toString();
		Otherinspect otherinspectform = otherinspectDao.selectOne(new LambdaQueryWrapper<Otherinspect>()
				.eq(Otherinspect::getBillNo,billNo));
		List<OtherInspectDetail> otherinspectList = otherInspectDetailDao.selectList(new LambdaQueryWrapper<OtherInspectDetail>()
				.eq(OtherInspectDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",otherinspectform);
		data.put("dataList",otherinspectList);
		return data;
	}



	@Override
	public Map<String, Object> getInspectDetailPhone(Map<String, Object> map) {
		String billNo = map.get("billNo").toString();
		Otherinspect inspectform = otherinspectDao.selectOne(new LambdaQueryWrapper<Otherinspect>()
				.eq(Otherinspect::getBillNo,billNo)
				.eq(Otherinspect::getStatus,"save"));
		if (null == inspectform){
			throw new RuntimeException("单号错误请重试");
		}
		List<OtherInspectDetail> inspectList = otherInspectDetailDao.selectList(new LambdaQueryWrapper<OtherInspectDetail>()
				.eq(OtherInspectDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",inspectform);
		data.put("dataList",inspectList);
		return data;
	}

	@Override
	public boolean changeOtherInspectIsChecked(String userName, Map<String, Object> map) {
		boolean isChecked = VtuzxUtil.getBoolean(map,"isChecked");
		Map<String,Object> insMap = VtuzxUtil.getObject(map,"val");
		Long detailId = VtuzxUtil.getLong(insMap,"id");
		Double unqualifiedQty = VtuzxUtil.getDouble(insMap,"unqualifiedQty");//不合格数量
		Double qualifiedQty = VtuzxUtil.getDouble(insMap,"qualifiedQty");//合格数量
		OtherInspectDetail detail = otherInspectDetailDao.selectById(detailId);
		detail.setUnqualifiedQty(unqualifiedQty);
		detail.setIsChecked(isChecked);
		detail.setQualifiedQty(qualifiedQty);
		boolean res = otherInspectDetailService.updateById(detail);

		return res;
	}

	@Override
	public boolean auditOtherInspect(String userName, Map<String, Object> map) throws Exception {
		String billNo = VtuzxUtil.getString(map,"billNo");

		//查询表头数据，准备修改质检状态以及质检人
		QueryWrapper<Otherinspect> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("billNo",billNo);
		Otherinspect inspect= otherinspectDao.selectOne(queryWrapper);
		if (inspect == null){
			throw new RuntimeException("单号错误请重试");
		}
		inspect.setStatus("已质检");
		inspect.setInspectUser(userName);
		inspect.setInspectDate(new Date());
		Boolean sendRes = false;//是否发送待办通知
		//查询表体，准备修改质检合格与不合格数量
		QueryWrapper<OtherInspectDetail> purInspectDetailQueryWrapper = new QueryWrapper<>();
		purInspectDetailQueryWrapper.eq("billNo",billNo);
		List<OtherInspectDetail> purInspectDetailList = otherInspectDetailDao.selectList(purInspectDetailQueryWrapper);
		List<OtherInspectDetail> otherInList = new ArrayList<>();//需要生成其他入库的
		for (OtherInspectDetail detail :purInspectDetailList){

			Double qualifiedQty = detail.getQualifiedQty();//质检数量
			Double inspectQty = detail.getInspectQty();
			Double unqualifiedQty = detail.getUnqualifiedQty();//不合格数量
			if (unqualifiedQty != 0){
				detail.setResult("不合格");
			}else {
				detail.setResult("合格");
			}
			//TODO发送待办通知

			//质检全部不合格不生成采购入库单
			if (inspectQty > unqualifiedQty){
				otherInList.add(detail);
			}

		}

		if (otherInList.size() > 0 && null != otherInList){
			try {
				//质检后生成其他入库单
				Boolean purInRes = otherInService.createOtherIn(inspect,otherInList,userName);
			} catch (Exception e){
				throw e;
			}
		}


		boolean resdetail = otherInspectDetailService.saveOrUpdateBatch(purInspectDetailList);

		boolean reshead = otherinspectService.saveOrUpdate(inspect);
		return reshead & resdetail;
	}


}